package com.java.util;

import com.java.Enum.PopularityAnalysis;
import org.apache.hadoop.yarn.webapp.hamlet.Hamlet;
import scala.collection.JavaConversions;
import scala.collection.Map$;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: ZhangJin
 * @Date: 2020/8/28 15:36
 * 数据处理工具类
 *
 */

/* 数据处理正则表达式   ¥60*/
public class PatternUtil {

    /* 规定后边为1-4位数字 */
    public static String pattern1 = "^¥\\d{1,4}$";

    /* 评分分数正则 */
    public static String pattern2 = "(^[1-5]分$)|(^[1-5]+(\\.\\d+)分$)";


    /* 数据处理 ----> 附近商店数据 */
   public static Boolean MatchPatternForNearByStore(String row1,String row2){
       return Pattern.matches(pattern1,row1)&&Pattern.matches(pattern2,row2);
   }
    /* 数据处理 ----> 租赁商店数据 */

    // TODO: 2020/9/2   1:处理详细位置信息,若数据中不包含地域位置、那么加上
//                      2：若是每月的价格低于50,说明单位为万  X10000

    public static String SplitString(String Redax,String content){
        return content.split(Redax)[1];
    }

    public static String PopularityAnalysis(String i){
        if (!i.equals("")) {
            int PopularityIndex = Integer.parseInt(i);
            if (PopularityIndex >= 500) {
                return PopularityAnalysis.Popularity1.getExplain();
            }
            if (PopularityIndex <= 500 && PopularityIndex > 100) {
                return PopularityAnalysis.Popularity2.getExplain();
            }
            if (PopularityIndex <= 100 && PopularityIndex > 20) {
                return PopularityAnalysis.Popularity3.getExplain();
            }
            if (PopularityIndex <= 20) {
                return PopularityAnalysis.Popularity4.getExplain();
            }

        }
        return "没有匹配到响应的受欢迎程度";
    }

    //java的map转为scala的immutable.map
    public static scala.collection.mutable.Map<String,String> JavaMapToScala(Map kafkaparams) {
        Map<String, String> javamap = new HashMap<>();
        scala.collection.mutable.Map scalaMap = JavaConversions.mapAsScalaMap(javamap);
        Object objTest = Map$.MODULE$.<String,String>newBuilder().$plus$plus$eq(scalaMap.toSeq());
        Object resultTest = ((scala.collection.mutable.Builder) objTest).result();
        scala.collection.mutable.Map<String,String> scala_imMap = (scala.collection.mutable.Map)resultTest;
        return scala_imMap;
    }

    /* 提取字符串中的数字 */
    public static String deleteString(String a){
        if (a != null) {
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            Matcher matcher = p.matcher(a);
            return matcher.replaceAll("").trim();
        }
        // TODO: 2020/9/20   mistake data
        return "mistake data" ;
    }


    public static String PriceTransfer(String a){
        Double PriceByMouth = null;
        try {
            PriceByMouth  = Double.parseDouble(a);
        }catch (Exception e){
            /* 由于是 面议 经过处理变为空字符串  捕捉异常 */
            PriceByMouth = 0.0;
        }
         if (PriceByMouth < 100 && PriceByMouth != 0.0)
         {
             /* 说明是以万为单位  进行转化 */
             return String.valueOf(PriceByMouth * 1000);
         }

        return String.valueOf(PriceByMouth);
    }

    // TODO: 2020/9/21   随机生成字符串
    public static String RandomString()  {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for( int i = 0; i < 8; i ++) {
            int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写还是小写
            stringBuilder.append((char)(choice + random.nextInt(26)));
        }
        return stringBuilder.toString().toLowerCase();
    }

}
